<?php

class GoodsAction extends SecurityAction{
	function _initialize(){
		$this->assign("menuFirst",session('menuFirst'));
		$this->assign("menuSecond",session('menuSecond'));
	}
	
	function glist(){
		$keyWord = $_GET["keyWord"];
		
		if($keyWord){
			$sql[] = array('goods_code'=>array('like',"%".$keyWord."%"));
			$sql['_logic'] = 'or';
			$sql[] = array('goods_name'=>array('like',"%".$keyWord."%"));
		}
		$ob = M("yq_goods");
		
		$cpp = 20; //分页数
		import('ORG.Util.Page');// 导入分页类
		$count      = $ob->where($sql)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$list = $ob->where($sql)->order(array('ctime'=>'desc'))->limit($Page->firstRow.','.$Page->listRows)->select();
	
		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出
	
		$this->display("Goods/glist");
	}
	
	function detail(){
		if ($_POST){
			$ob = M("yq_goods");

			if ($_POST['tid'] == ''){
				$info = $ob->where(array("goods_code"=>$_POST["goods_code"]))->select();
				if ($info){
					$this->error("物品编码已经存在!"); //,"/Goods/glist"
					return;
				}else{
					$ob->add($_POST);
				}
			}else{
				$ob->where(array("tid"=>$_POST["tid"]))->save($_POST);
			}
			
			$this->redirect("Goods/glist");
			return;
		}
		
		if ($_GET['opt'] == 'add'){  //新增跳转
			$this->display('Goods/detail');
			return;
		}
		
		if ($_GET["opt"] == 'edit'){   //编辑
			$ob = M("yq_goods");
			$info = $ob->where(array("tid"=>$_GET["tid"]))->find();
			$this->assign("modify", '1');
			$this->assign("info", $info);
		
			$this->display('Goods/detail');
			return;
		}
	}
}

?>